<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: UserPermissions.php 323 2008-12-01 18:32:53Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalPageRecord');

class UserPermissions extends PortalAdminPage
{
	public function getPages()
	{
		return PortalPageRecord::finder()->findAll('order by path');
	}

	public function getGroupData()
	{
		// Guest user have id 0 - and is not in the user table
		$guser = $this->DataAccess->createCommand("select tblpermissions.*, 'Guest' as username from tblpermissions where selector='user' && tblpermissions.value=0 order by page")->query();
		$nuser = $this->DataAccess->createCommand("select tblpermissions.*,tblusers.name as username from tblpermissions,tblusers where selector='user' && tblusers.id = tblpermissions.value order by page")->query();

		$allusers = array();

		if(is_array($guser))
		{
			foreach($guser as $g)
			{
				$allusers[] = $g;
			}
		}

		if(is_array($nuser))
		{
			foreach($nuser as $n)
			{
				$allusers[] = $n;
			}
		}

		return $allusers;
	}

	public function getUsers()
	{
		$allusers = array(
			0 => array("id" => 0, "name" => "Guest")
		);

		$users = $this->DataAccess->createCommand("select id,name from tblusers order by name")->query();
		if(is_array($users))
		{
			foreach($users as $u)
			{
				$allusers[] = $u;
			}
		}

		return $allusers;
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('User permissions');

		if($this->IsPostBack)
		{
			return;
		}

		$this->DataList->DataSource = $this->GroupData;
		$this->DataList->dataBind();

		$this->UserId->DataSource = $this->Users;
		$this->UserId->dataBind();

		$this->dlPage->DataSource = $this->getPages();
		$this->dlPage->dataBind();
	}

	public function editItem($sender, $param)
	{
		$this->DataList->SelectedItemIndex = -1;
		$this->DataList->EditItemIndex = $param->Item->ItemIndex;
		$this->DataList->DataSource = $this->GroupData;
		$this->DataList->dataBind();
	}

	public function cancelItem($sender, $param)
	{
		$this->DataList->SelectedItemIndex = -1;
		$this->DataList->EditItemIndex = -1;
		$this->DataList->DataSource = $this->GroupData;
		$this->DataList->dataBind();
	}

	public function updateItem($sender, $param)
	{
		$item	= $param->Item;
		$id		= $this->DataList->DataKeys[$item->ItemIndex];
		$page	= $item->dlEditPage->SelectedValue;
		$userid	= $item->dlEditRole->SelectedValue;
		$view	= $item->CanView->SelectedValue;
		$edit	= $item->CanEdit->SelectedValue;

		if($page == "All")
		{
			$sql = "update tblpermissions set page=null,view='$view',edit='$edit',value='$userid' where id='$id'";
		}
		else
		{
			$sql = "update tblpermissions set page='$page',view='$view',edit='$edit',value='$userid' where id='$id'";
		}

		$this->DataAccess->createCommand($sql)->execute();
		$this->DataList->EditItemIndex	= -1;
		$this->DataList->DataSource		= $this->GroupData;
		$this->DataList->dataBind();
	}

	public function deleteItem($sender, $param)
	{
		$item	= $param->Item;
		$id		= $this->DataList->DataKeys[$item->ItemIndex];
		$this->DataAccess->createCommand("delete from tblpermissions  where id='$id'")->execute();
		$this->DataList->EditItemIndex		= -1;
		$this->DataList->SelectedItemIndex	= -1;
		$this->DataList->DataSource			= $this->GroupData;
		$this->DataList->dataBind();
	}

	public function AddUser($sender, $param)
	{
		$page	= $this->dlPage->SelectedValue;
		$userid = $this->UserId->SelectedValue;
		$view	= $this->CanView->SelectedValue;
		$edit	= $this->CanEdit->SelectedValue;

		$this->DataAccess->createCommand("insert tblpermissions(page,selector,value,view,edit) values('$page','user','$userid','$view','$edit')")->execute();
		$this->dlPage->SelectedValue	= 'All';
		$this->DataList->DataSource		= $this->GroupData;
		$this->DataList->dataBind();
	}

	public function itemCreated($sender, $param)
	{
		$item = $param->Item;

		if ($item->ItemType === 'EditItem')
		{
			$item->dlEditPage->DataSource = $this->getPages();
			$item->dlEditRole->DataSource = $this->getUsers();
		}
	}
}
?>